import request from "@/utils/request"

export default {
    namespaced: true,
    state: {
        cart: JSON.parse(localStorage.getItem('cart') || '[]'),

    },
    actions: {

    },
    mutations: {
        // 加入购物车
        addToCart(state, good) {
            request.post('/cart/cartAdd', good).then(res => {
                if (res.code == 20000) {
                    this.commit('cartStore/getCartData')
                }
            })
        },
        // 获取购物车商品信息
        getCartData(state) {
            request.get('/cart/cartPage/1/100000').then(res => {
                if (res.code == 20000) {
                    state.cart = res.data.list
                    this.commit('cartStore/saveCart')
                }
            })
        },
        // 删除购物车的商品
        delectCartGoods(state, id) {
            request.delete('/cart/cartDel?id=' + id).then(res => {
                if (res.code == 20000) {
                    this.commit('cartStore/getCartData')
                }
            })
        },
        // 修改购物车商品
        editCartGoods(state, good) {
            request.put('/cart/cartEdit', good).then(res => {
                if (res.code == 20000) {
                    const findResult = state.cart.find(item => item.productId === good.productId)
                    if (findResult) {
                        findResult.productQty = good.productQty
                        this.commit('cartStore/saveCart')

                    }
                }
            })
        },
        saveCart(state) {
            localStorage.setItem('cart', JSON.stringify(state.cart))
        },
        // 改变商品勾选状态
        changeCheckState(state, good) {
            const findResult = state.cart.find(item => item.productId === good.productId)
            if (findResult) {
                findResult.checked = good.checked
            }
            this.commit('cartStore/saveCart')

        },
        CheckAllChange(state, newState) {
            state.cart.forEach(item => item.checked = newState);
            this.commit('cartStore/saveCart')

        },
        // 购物车所有商品全部勾选时 全部 勾选框 也勾选
        checkedAll(state) {
            state.cart.forEach(item => item.checked)
        },
        // 删除商品
        delect(state, ids) {
            request.delete('/cart/cartDel' + ids).then(res => {
                if (res.code == 2000) {
                }
            })
            // state.cart = state.cart.filter(item => item.id !== ids)
            // this.commit('cartStore/saveCart')
        }
    },
    getters: {

        checkCount(state) {
            // filter:过来购物车中已经勾选的商品 reduce：将勾选的商品累加
            return state.cart.filter(item => item.checked).reduce((totalcount, i) => totalcount += i.productQty, 0)

        },
        sum(state) {
            return state.cart.filter(item => item.checked).reduce((amount, item) => amount += item.productQty *
                item.price, 0).toFixed(2)
        },
        checkedArr(state) {
            return state.cart.filter(item => item.checked === true)

        }
    }
}